<template>
  <div>
    <van-nav-bar
      title="订单详情"
      @click-left="goBack()"
      left-text="返回"
      left-arrow
      :border="false"
    >
      <template #right>
        <van-icon name="ellipsis" size="25" />
      </template>
    </van-nav-bar>
    <div class="order_state">
      <div class="state_txt">
        <div class="icon state_time"></div>
        等待付款
      </div>
      <div class="state_tips">
        <div class="tip_item">应付金额：¥{{ amount }}.00</div>
        <div class="tip_item">支付剩余00小时{{ time }}分</div>

        <!---->
      </div>
      <div data-id="payNormal" @click="addAllorder" class="state_btn">
        去支付
      </div>
    </div>
    <div class="order_wuliu">
      <div data-href="" data-progid="address" class="wuliu_cont">
        <div class="icon wl_pos_icon"></div>
        <div class="cont_text">
          <div class="wl_text"><!---->王增威 183****4821</div>
          <span class="wl_tip"
            >湖南永州市冷水滩区梧桐街道凤凰路779号永州职业技术学院农学部</span
          >
        </div>
        <!---->
        <!---->
      </div>
    </div>

    <div class="con_2" v-for="item in detaisList" :key="item.recID">
      <van-cell class="con_title" title="京东自营">
        <template #right-icon>
          <van-tag color="#ffe1e1" text-color="#ad0000"
            >共计{{ item.goodsNum }}件</van-tag
          >
        </template>
      </van-cell>
      <van-card
        :num="item.goodsNum"
        :price="item.GoodsPrice + '.00'"
        :desc="'颜色:' + item.Color + ',尺码/版本:' + item.Size"
        :title="item.GoodName"
        :thumb="item.GoodsPic"
      >
        <template #tags>
          <van-tag plain type="danger">精品好货</van-tag>
        </template>
      </van-card>
      <van-cell class="con_title_2">
        <template #title> </template>
        <template #right-icon>
          <van-button
            style="width: 110px; height: 35px"
            round
            color="linear-gradient(to right, #ff6034, #ee0a24)"
          >
            去支付
          </van-button>
        </template>
      </van-cell>
    </div>
    <van-collapse v-model="activeNames">
      <van-collapse-item name="1">
        <template #title>
          <span>订单编号：{{ OrderNum }}</span>
        </template>
        <template #right-icon>
          <span>应付金额：￥{{ amount }}</span>
        </template>
        
      </van-collapse-item>
    </van-collapse>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { setItem, getItem, removeItem } from "../utils/storage";

export default {
  data() {
    return {
      activeNames: ["0"],
      detaisList: [],
    };
  },
  mounted() {},
  created() {
    this.getOrder();
  },
  activated() {
    let myEndTime = getItem("myEndTime");
    myEndTime && this.codeCountDown(myEndTime);
  },

  methods: {
    async getOrder() {
      const { data: res } = await this.$http.get("getOrder", {
        params: { num: this.OrderNum },
      });
      console.log(res);
      this.detaisList = res.data;
    },
    codeCountDown() {
      let r = getItem("myEndTime");
      this.countDownTime = Math.ceil((r - new Date().getTime()) / 1000);
      let times = Math.floor(this.countDownTime / 60);
      this.$store.commit("setTime", times);

      console.log(times);
      let time = setTimeout(() => {
        this.countDownTime--;

        if (this.countDownTime < 1) {
          if (times == 0) {
            this.removeOrder();
            this.addAllorder();
          }
          
          window.localStorage.removeItem("myEndTime");
          window.localStorage.removeItem("time");
          window.localStorage.removeItem("OrderNum");

        //   this.addAllorder();
        //   this.removeOrder();

          
          clearTimeout(time);
        } else {
          this.codeCountDown(r);
        }
      }, 1000);
    },
    async removeOrder() {
      const { data: res } = await this.$http.get("deleOrder", {
        params: { num: this.OrderNum },
      });
      console.log(res);
    },
    addAllorder() {
      this.addReceived();
    },
    async addReceived() {
      var arry2 = [];

      var date = new Date();

      var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
      var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
      var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)

      var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
      var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
      var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
      var currentFormatDate =
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds;
      this.recList.map((item, index) => {
        arry2.push(Object.assign({}, item, { newTime: currentFormatDate,orderState:'已取消' }));
      });

      const { data: res } = await this.$http.post("addAllOrder", arry2);

      console.log(res);
    },
    goBack() {
      
      this.$router.go(-1);
    },
  },
  computed: {
    ...mapState(["OrderNum", "emailLogin", "time", "recList"]),
    amount() {
      let a = 0;
      this.detaisList.forEach((x) => {
        a += x.GoodsPrice * x.goodsNum;
      });
      return a;
    },
  },
  watch: {
    OrderNum() {
      console.log(this.OrderNum);
      this.getOrder();
    },
  },
};
</script>
<style lang="less" scoped>
.order_state {
  position: relative;
  background: url(../assets/icon/b3.png) repeat-y;
  color: #fff;
  padding: 64px 10px 30px;
  font-size: 13px;
  text-align: center;
  overflow: hidden;
  border: 1px solid;
  .state_txt {
    position: relative;
    font-size: 18px;
    line-height: 20px;
    font-weight: 500;
    z-index: 1;
    margin-bottom: 8px;
  }
  .icon {
    display: inline-block;
    width: 20px;
    height: 20px;
    margin-right: 5px;
    background-image: url(https://img20.360buyimg.com/img/s80x80_jfs/t1/190532/19/15828/893/61029c93Eee4a7bad/a69e6e4fd423b1ba.png);
    background-size: 40px;
    background-repeat: no-repeat;
    vertical-align: -3px;
  }
  .state_time {
    background-position: 0 -20px;
  }
  .tip_item {
    position: relative;
    margin-top: 6px;
    display: inline-block;
    z-index: 1;
    text-align: left;

    margin-left: 15px;
  }
  .state_btn {
    margin-top: 15px;
    display: inline-block;
    background: #fff;
    box-shadow: 0 8px 16px 0 rgb(231 0 28 / 14%),
      inset 0 -4px 6px 0 rgb(255 198 198 / 77%);
    border-radius: 20px;
    position: relative;
    z-index: 1;
    color: #f2270c;
    padding: 10px 38px;
    line-height: 14px;
  }
}
.order_state + .order_wuliu {
  margin-top: -10px;
}
.order_wuliu {
  position: relative;
  background-color: #fff;
  border-radius: 10px;
  .wuliu_cont {
    padding: 11px 10px 12px;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    position: relative;
  }
  .wl_pos_icon {
    background-position: -65px -22px;
    width: 16px;
    height: 19px;
  }
  .icon {
    display: inline-block;
    width: 20px;
    height: 20px;
    margin-right: 10px;
    background-image: url(https://wq.360buyimg.com/fd/h5/wxsq_dev/order/images/sprite_detail_xcxv2@2x_1553f337.png);
    background-size: 82.5px 79px;
    background-repeat: no-repeat;
  }
  .cont_text {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    position: relative;
  }

  .wl_text {
    font-size: 14px;
    line-height: 21px;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .wl_tip {
    font-size: 12px;
    line-height: 18px;
    color: #999;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
}
.con_2 {
  padding: 10px;
}
.con_title {
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
}
.van-card {
  margin-top: -1px;

  background: #fff;
}
.custom-title {
  line-height: 2.9rem;
}
.con_title_2 {
  border-bottom-right-radius: 10px;
  border-bottom-left-radius: 10px;
}
</style>